package com.nuaa.yygh.hosp.api;

import com.nuaa.model.hosp.Department;
import com.nuaa.model.hosp.Hospital;
import com.nuaa.model.hosp.Schedule;
import com.nuaa.vo.hosp.DepartmentQueryVo;
import com.nuaa.vo.hosp.ScheduleQueryVo;
import com.nuaa.yygh.common.handler.YyghException;
import com.nuaa.yygh.common.helper.HttpRequestHelper;
import com.nuaa.yygh.common.result.Result;
import com.nuaa.yygh.common.result.ResultCodeEnum;
import com.nuaa.yygh.common.util.MD5;
import com.nuaa.yygh.hosp.service.DepartmentService;
import com.nuaa.yygh.hosp.service.HospitalService;
import com.nuaa.yygh.hosp.service.HospitalSetService;
import com.nuaa.yygh.hosp.service.ScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")

//用来写平台对外的开发
//就是让别人来添加医院信息之类的
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;


    @Autowired
    private HospitalSetService hospitalSetService;

    //1.上传医院接口

    @ApiOperation(value = "1.上传医院接口")
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将String[]数组转成Object,省去等下遍历的过程
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1.得到医院的编码,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编码查询数据库，查签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询的签名也进行MD5的加密
        String signKeyMd5 = MD5.encrypt(signKey);

        if (StringUtils.isEmpty(hoscode)) {
            //抛出没有编码的异常
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        //4.做签名校验
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中“+”转为“ ”，要进行转换  logo
        String logoDataString  = (String) paramMap.get("logoData");
        String logoData = logoDataString .replaceAll(" ", "+");
        paramMap.put("logoData", logoData);
        //调用service方法
        hospitalService.save(paramMap);
        return Result.ok();
    }

    //2.获取医院信息
    @ApiOperation(value = "2.获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将String[]数组转成Object,省去等下遍历的过程
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1.得到医院的编码,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编码查询数据库，查签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询的签名也进行MD5的加密
        String signKeyMd5 = MD5.encrypt(signKey);

        if (StringUtils.isEmpty(hoscode)) {
            //抛出没有编码的异常
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        //4.做签名校验
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);

        return Result.ok(hospital);
    }


    //3.上传科室
    @ApiOperation(value = "3.上传科室")
    @PostMapping("saveDepartment")
    private Result saveDepartment(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将String[]数组转成Object,省去等下遍历的过程
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1.得到医院的编码,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编码查询数据库，查签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询的签名也进行MD5的加密
        String signKeyMd5 = MD5.encrypt(signKey);

        if (StringUtils.isEmpty(hoscode)) {
            //抛出没有编码的异常
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        //4.做签名校验
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法
        departmentService.save(paramMap);
        return Result.ok();
    }


    //4.获取分页列表
    @ApiOperation(value = "4.获取分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //1.得到医院的编码,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编码查询数据库，查签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String depcode = (String) paramMap.get("depcode");
        //页码如果是空的话就从第一页开始，不然就是page
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        //3.把数据库查询的签名也进行MD5的加密
        String signKeyMd5 = MD5.encrypt(signKey);

        if (StringUtils.isEmpty(hoscode)) {
            //抛出没有编码的异常
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        //4.做签名校验
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        Page<Department> page1 = departmentService.selectPage(page, limit, departmentQueryVo);
        return Result.ok(page1);
    }

    //5.删除科室
    @ApiOperation(value = "5.删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //1.得到医院的编码,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编码查询数据库，查签名
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询的签名也进行MD5的加密
        String signKeyMd5 = MD5.encrypt(signKey);

        if (StringUtils.isEmpty(hoscode)) {
            //抛出没有编码的异常
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //4.做签名校验
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    //上传排班信息
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //1.得到医院的编码,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编码查询数据库，查签名
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询的签名也进行MD5的加密
        String signKeyMd5 = MD5.encrypt(signKey);

        if (StringUtils.isEmpty(hoscode)) {
            //抛出没有编码的异常
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //4.做签名校验
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //1.得到医院的编码,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编码查询数据库，查签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String depcode = (String) paramMap.get("depcode");
        //页码如果是空的话就从第一页开始，不然就是page
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        //3.把数据库查询的签名也进行MD5的加密
        String signKeyMd5 = MD5.encrypt(signKey);

        if (StringUtils.isEmpty(hoscode)) {
            //抛出没有编码的异常
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        //4.做签名校验
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> page1 = scheduleService.selectPage(page,limit,scheduleQueryVo);
        return Result.ok(page1);
    }


    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //1.得到医院的编码,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编码查询数据库，查签名
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询的签名也进行MD5的加密
        String signKeyMd5 = MD5.encrypt(signKey);

        if (StringUtils.isEmpty(hoscode)) {
            //抛出没有编码的异常
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //4.做签名校验
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();

    }
}